package pms;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import com.toedter.calendar.JDateChooser;

public class DatePicker {
	
	private static DateTime selectedDate;
	private static JDateChooser dateChooser;
	private static JSpinner timeSpinner; 
	
	public DatePicker()
	{
		
		
		
	}
	
	@SuppressWarnings("deprecation")
	public JPanel buildDatePanel(String label,DateTime date) {
		
		Date d = date.toDate();
		
		JPanel datePanel = new JPanel();
			
		dateChooser = new JDateChooser();
		if (date != null) {
		    dateChooser.setDate(d);
		    
		}
		for (Component comp : dateChooser.getComponents()) {
		    if (comp instanceof JTextField) {
			((JTextField) comp).setColumns(50);
			((JTextField) comp).setEditable(false);
		    }
		}

		datePanel.add(dateChooser);

		final SpinnerModel model = new SpinnerDateModel();
		timeSpinner = new JSpinner(model);
		JComponent editor = new JSpinner.DateEditor(timeSpinner, "HH:mm:ss");
		timeSpinner.setEditor(editor);
		if(date != null) {
		    timeSpinner.setValue(d);
		}
		
		timeSpinner.setAlignmentX(60);
		timeSpinner.setAlignmentY(60);
		datePanel.add(timeSpinner);
		
		 Object[] message = {label, dateChooser, timeSpinner};
				 Object[] options = {"OK", "Cancel"};
				 JOptionPane optionPane = new JOptionPane(message,
				 JOptionPane.PLAIN_MESSAGE,
				 JOptionPane.OK_CANCEL_OPTION,
				 null, options);
				 Component frame = null;
				JDialog dialog = optionPane.createDialog(frame, "Spinners");
				 dialog.show();
				return dateChooser;
		
		
//		dialog.addPropertyChangeListener(new ActionListener() {
//			 
//            public void actionPerformed(ActionEvent e)
//            {
//            	Date d = dateChooser.getDate();
//            	selectedDate = new DateTime(d);
//            	System.out.println(selectedDate.toString());
//            	    	
//            }
//        });      

		
		}
	
	public DateTime getSelectedDate()
	{
		Date d = dateChooser.getDate();
    	selectedDate = new DateTime(d);
    	DateTimeFormatter dat = DateTimeFormat.forPattern("yyyy-MM-dd");
    	DateTimeFormatter tdf = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss");
    	DateTimeFormatter tim = DateTimeFormat.forPattern("HH:mm:ss");
    	DateTimeFormatter fin = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    	DateTime dd = DateTime.parse(timeSpinner.getValue().toString().substring(0, timeSpinner.getValue().toString().length() - 9), tdf);
    	//System.out.println(selectedDate.toString(dat) + " " + dd.toString(tim));
    	
    	selectedDate = DateTime.parse(selectedDate.toString(dat) + " " + dd.toString(tim), fin);
    	
		return selectedDate;
	}
	
	public void close()
	{
		
	}
     	
}
